package wang.conge.javasedemo.core.thread;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class TryLockTest {

	public static void main(String[] args) {
		Lock lock = new ReentrantLock();

		Model model = new Model();

		Runnable numCountThread = (() -> {
			if (lock.tryLock()) {
				try {
					model.addNum();
					System.out.println(model.getNum());
				} finally {
					lock.unlock();
				}
			} else {
				System.out.println("nolock:" + model.getNum());
			}
		});

		for (int i = 1; i <= 10; i++) {
			new Thread(numCountThread).start();
		}
	}

	static class Model {
		private int num = 0;

		public int getNum() {
			return num;
		}

		public void addNum() {
			num = num + 1;
		}
	}

}
